package com.thenie.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thenie.pojo.Room;
import com.thenie.service.RoomService;
import com.thenie.utils.DataGridViewResult;
import com.thenie.utils.SystemConstant;
import com.thenie.vo.RoomVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin/room")
public class RoomController {

    @Resource
    private RoomService roomService;

    /**
     * 查询房间列表
     * @param roomVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(RoomVo roomVo){
        //设置分页
        PageHelper.startPage(roomVo.getPage(), roomVo.getLimit());
        //查询楼层列表
        List<Room> roomList = roomService.findRoomList(roomVo);
        //创建分页对象
        PageInfo<Room> floorPageInfo = new PageInfo<Room>(roomList);

        return new DataGridViewResult(floorPageInfo.getTotal(),floorPageInfo.getList());
    }

    /**
     * 添加一条房型
     * @param room
     * @return
     */
    @RequestMapping("/addRoom")
    public String addRoom(Room room){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i =roomService.addRoom(room);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 添加一条房型
     * @param room
     * @return
     */
    @RequestMapping("/updateRoom")
    public String updateRoom(Room room){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i =roomService.updateRoom(room);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 删除一条房型
     * @param id
     * @return
     */
    @RequestMapping("/deleteRoom")
    public String deleteRoom(int id){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i =roomService.deleteRoom(id);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

}
